package com.learn.eduadboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.eduadboot.service.PromotionSpaceService;
import com.learn.eduapi.entity.ResponseDTO;
import com.learn.eduapi.entity.ad.PromotionSpace;
import com.learn.eduapi.entity.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告位后台管理服务8001
 */
@RestController
@CrossOrigin
@RequestMapping("ad")
public class AdminAdSpaceController {
    @Autowired
    private PromotionSpaceService  promotionSpaceService;

    /**
     * 获取广告位列表
     * @param find
     * @param page
     * @param size
     * @return
     */
    @GetMapping("getAllPromotionSpace")
    public ResponseDTO<List<PromotionSpace>> getAllPromotionSpace(@RequestParam("find")String find,
                                                                  @RequestParam("page")Integer page,
                                                                  @RequestParam("size")Integer size){
        ResponseDTO<List<PromotionSpace>> responseDTO = new ResponseDTO<>();
        responseDTO.setContent(promotionSpaceService.getAllPromotionSpace(find,(page-1)*size,size));
        responseDTO.setState(promotionSpaceService.getAllPromotionSpace(find,0,0).size());
        responseDTO.setMessage("获取广告位列表成功");
        return responseDTO;
    }

    /**
     * 是否删除广告位
     * @param id
     * @param isDel
     * @return
     */
    @GetMapping("adIsDelChanged/{id}/{isDel}")
    public ResponseDTO<PromotionSpace> adIsDelChanged(@PathVariable("id")Integer id,
                                                      @PathVariable("isDel")Integer isDel){
        ResponseDTO<PromotionSpace> responseDTO = new ResponseDTO<>();
        QueryWrapper<PromotionSpace> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        PromotionSpace promotionSpace = new PromotionSpace();
        if (isDel.equals("1")){
            promotionSpace.setIsDel(0);
        }else {
            promotionSpace.setIsDel(1);
        }
        int i = promotionSpaceService.updatePromotionSpace(promotionSpace, wrapper);
        if (i==1){
            responseDTO.setState(200);
        }else {
            responseDTO.setState(500);
        }
        return responseDTO;
    }

    /**
     * 修改广告位详情
     * @param id
     * @param name
     * @param spaceKey
     * @return
     */
    @GetMapping("updateAdAll")
    public ResponseDTO<PromotionSpace> updateadAll(@RequestParam("id")String id,
                                                   @RequestParam("name")String name,
                                                   @RequestParam("spaceKey")String spaceKey){
        ResponseDTO<PromotionSpace> responseDTO = new ResponseDTO<>();
        PromotionSpace promotionSpace1 = promotionSpaceService.selectBy(name, null);
        PromotionSpace promotionSpace2 = promotionSpaceService.selectBy(null, spaceKey);
        if (promotionSpace1!=null && promotionSpace1.getId()!=Integer.parseInt(id)){
            responseDTO.setState(500);
            responseDTO.setMessage("广告位名称重复");
        }else if (promotionSpace2!=null && promotionSpace2.getId()!=Integer.parseInt(id)){
            responseDTO.setState(500);
            responseDTO.setMessage("广告位Key重复");
        }else {
            QueryWrapper<PromotionSpace> wrapper = new QueryWrapper<>();
            wrapper.eq("id", id);
            PromotionSpace promotionSpace = new PromotionSpace();
            promotionSpace.setName(name);
            promotionSpace.setSpaceKey(spaceKey);
            int i = promotionSpaceService.updatePromotionSpace(promotionSpace, wrapper);
            if (i==1){
                responseDTO.setState(200);
            }else {
                responseDTO.setState(500);
                responseDTO.setMessage("广告位修改失败");
            }
        }
        return responseDTO;
    }

    /**
     * 添加广告位
     * @param name
     * @param spaceKey
     * @return
     */
    @GetMapping("addAdAll")
    public ResponseDTO<PromotionSpace> addAdAll(@RequestParam("name")String name,
                                                @RequestParam("spaceKey")String spaceKey){
        ResponseDTO<PromotionSpace> responseDTO = new ResponseDTO<>();
        PromotionSpace promotionSpace1 = promotionSpaceService.selectBy(name, null);
        PromotionSpace promotionSpace2 = promotionSpaceService.selectBy(null, spaceKey);
        if (promotionSpace1!=null){
            responseDTO.setState(500);
            responseDTO.setMessage("广告位名称重复");
        }else if (promotionSpace2!=null){
            responseDTO.setState(500);
            responseDTO.setMessage("广告位Key重复");
        }else {
PromotionSpace promotionSpace = new PromotionSpace();
promotionSpace.setName(name);
promotionSpace.setSpaceKey(spaceKey);
            int i = promotionSpaceService.addSpace(promotionSpace);
            if (i==1){
                responseDTO.setState(200);
            }else {
                responseDTO.setState(500);
                responseDTO.setMessage("广告位新增失败");
            }
        }
        return responseDTO;
    }

    /**
     * 真正删除广告位
     * @param id
     * @return
     */
    @GetMapping("removeAdSpace/{id}")
    public ResponseDTO<PromotionSpace> removeAdSpace(@PathVariable("id")Integer id){
        ResponseDTO<PromotionSpace> responseDTO = new ResponseDTO<>();
        int i = promotionSpaceService.removeAdSpace(id);
        if (i==1){
            responseDTO.setState(200);
        }else {
            responseDTO.setState(500);
            responseDTO.setMessage("广告位真正删除失败");
        }
        return  responseDTO;
    }

}
